/* eslint-disable */
import EventBus from './event_bus';

let hasReachBottom = false;

let onReachBottom = true;

const onReachBottomDistance = 150;

function isReachBottom() {
  const {
    clientHeight,
    scrollHeight,
  } = document.documentElement;
  const { scrollY } = window;

  const isBottom = scrollY > 0 && scrollHeight > clientHeight && (scrollY + clientHeight + onReachBottomDistance) >=
    scrollHeight;
  if (isBottom && !hasReachBottom) {
    hasReachBottom = true
    return true
  }

  if (!isBottom && hasReachBottom) {
    hasReachBottom = false
  }
  return false
}

window.addEventListener('scroll', () => {
  if (onReachBottom && isReachBottom()) {
    EventBus.$emit('reachBottom');
    onReachBottom = false;
    setTimeout(() => {
      onReachBottom = true
    }, 350)
  }
});
